package vsm.vsm_server.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vsm.vsm_server.mapper.CommentMapper;
import vsm.vsm_server.pojo.Comment.*;
import vsm.vsm_server.pojo.User.UserInfo;
import vsm.vsm_server.service.CommentService;
import vsm.vsm_server.service.NoticeService;
import vsm.vsm_server.service.UserService;
import vsm.vsm_server.service.VodService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

     private CommentMapper commentMapper;
     public CommentServiceImpl(CommentMapper commentMapper){this.commentMapper = commentMapper;}
    @Autowired
    UserService userService;
    @Autowired
    VodService vodService;
    @Autowired
    NoticeService noticeService;
    @Override
    public CommentRootVo getRootCommentByVideoId(RootCommentGetDto rootCommentGetDto) {
        CommentRootVo commentRootVo = new CommentRootVo();
        List<CommentRoot> commentRootList = commentMapper.getRootCommentsByVideoId(rootCommentGetDto);
        if(!commentRootList.isEmpty()){
            List<BigInteger> userInfoList = new ArrayList<>();
            for(CommentRoot commentRoot : commentRootList){
                userInfoList.add(commentRoot.getUserId());
            }
            Map<BigInteger,UserInfo> userInfos =userService.getUserInfoList(userInfoList);
            for(CommentRoot c : commentRootList){
                UserInfo userInfo = userInfos.get(c.getUserId());
                BeanUtils.copyProperties(userInfo,c);
            }
        }
        commentRootVo.setCommentList(commentRootList);
        commentRootVo.setTotalCount(commentMapper.getCommentCount(rootCommentGetDto.getVideoId()));
        return commentRootVo;
    }
    @Override
    public CommentChildVo getChildCommentListByRootId(ChildCommentGetDto childCommentGetDto) {
        CommentChildVo commentChildVo = new CommentChildVo();
        List<CommentChild> commentChildList = commentMapper.getChildCommentsByRootId(childCommentGetDto);
        if(!commentChildList.isEmpty()){
            List<BigInteger> userInfoList = new ArrayList<>();
            for(CommentChild commentChild: commentChildList){
                userInfoList.add(commentChild.getUserId());
            }
            Map<BigInteger,UserInfo> userInfos =userService.getUserInfoList(userInfoList);
            for(CommentChild c : commentChildList){
                UserInfo userInfo = userInfos.get(c.getUserId());
                BeanUtils.copyProperties(userInfo,c);
            }
        }
        commentChildVo.setRootCommentId(childCommentGetDto.getRootCommentId());
        commentChildVo.setCommentList(commentChildList);
        return commentChildVo;
    }

    @Override
    public Double getCommentCount(BigInteger videoId) {
        return commentMapper.getCommentCount(videoId);
    }

    @Override
    public BigInteger getCommentSenderId(BigInteger commentId) {
        return commentMapper.getCommentSenderId(commentId);
    }

    @Override
    public CommentDetail sendComment(CommentSendDto commentSendDto) {
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentSendDto,comment);
        commentMapper.sendComment(comment);
        CommentDetail commentDetail = new CommentDetail();
        BeanUtils.copyProperties(comment,commentDetail);
        UserInfo userInfo = userService.getInfoById(commentDetail.getUserId());
        userInfo.setAvatar(vodService.getPicture(userInfo.getAvatar()));
        BeanUtils.copyProperties(userInfo,commentDetail);

        noticeService.saveCommentNotice(commentSendDto);

        return commentDetail;
    }


}
